home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Classes / RCString / RCStringMisc.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  116 lines

  1. #import <RCString.h>
  2. /*
  3.     Copyright (C) 1992. Bruce Ediger.
  4.  
  5.       This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU Library General Public License.
  7. */
  8.  
  9. //
  10. // category of "miscellaneous" methods.
  11. //
  12.  
  13. @implementation RCString (Misc)
  14.  
  15. - empty
  16. {
  17.     if (p->n > 1)
  18.         [self copyReference];
  19.  
  20.     if (p->s) {
  21.         free(p->s);
  22.         p->s = NULL;
  23.     }
  24.     p->l = 0;
  25.     return self;
  26. }
  27.  
  28. - toUpper
  29. {
  30.     int icIndex;
  31.  
  32.     if (p->n > 1)
  33.         [self copyReference];
  34.  
  35.     if (p->l > 1)
  36.         for (icIndex = 0; icIndex < p->l; ++icIndex) {
  37.             if (p->s[icIndex] <= 'z' && p->s[icIndex] >= 'a')
  38.                 p->s[icIndex] = p->s[icIndex] - 'a' + 'A'; 
  39.         }
  40.     return self;
  41. }
  42.  
  43. - toLower
  44. {
  45.     int icIndex;
  46.  
  47.     if (p->n > 1)
  48.         [self copyReference];
  49.  
  50.     if (p->l > 1)
  51.         for (icIndex = 0; icIndex < p->l; ++icIndex) {
  52.             if (p->s[icIndex] <= 'Z' && p->s[icIndex] >= 'A')
  53.                 p->s[icIndex] = p->s[icIndex] - 'A' + 'a'; 
  54.         }
  55.     return self;
  56. }
  57.  
  58. - replaceWithAsciiString: (char *)aString
  59. {
  60.     if(--p->n == 0)
  61.         free(p->s);
  62.     else
  63.         p = (struct srep *) malloc(sizeof(struct srep));
  64.  
  65.     if (p) {
  66.         p->n = 1;
  67.         if (aString)
  68.             p->l = strlen(aString) + 1;
  69.         else
  70.             p->l = 1;
  71.         p->s = malloc(p->l);
  72.         if (p->s && aString)
  73.                 bcopy(aString, p->s, p->l);
  74.         else if (p->s && aString == NULL)
  75.             *(p->s) = '\0';
  76.     }
  77.     return self;
  78. }
  79.  
  80. // replace this objects internal string rep with another
  81. // object's internal string rep.
  82. - replaceWithObject: (RCString *)anObject
  83. {
  84.     if (p->n == 1) {
  85.         // this object is the only reference. it's safe
  86.         // to just free() the whole internal rep
  87.         if (p->s)
  88.             free(p->s);
  89.         free(p);
  90.     } else {
  91.         --p->n;  // decrement ref count of previous internal rep
  92.     }
  93.     p = anObject->p;
  94.     ++p->n;
  95.     return self;
  96. }
  97.  
  98. // A hook into object to perform some arbitrary 
  99. // processing on the object's ASCIIZ string.
  100. // This may not be too clever.
  101. - performArbitraryFunction:(int (*)())someFunction
  102. {
  103.     if (someFunction != NULL) {
  104.         if (p->n > 1)
  105.             [self copyReference];
  106.  
  107.         if ((*someFunction)(p->s)) {
  108.             p->l = strlen(p->s);
  109.         }
  110.     }
  111.  
  112.     return self;
  113. }
  114.  
  115. @end
  116.